package top.guinguo.advance_04.searcher.util;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.*;

import java.io.IOException;

/**
 * Created by guin_guo on 2017/1/3.
 */
public class CustomFilterTest {
    public void searcherByCustomFilter() {
        try {
            IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
            Query query = new TermQuery(new Term("content", "java"));
            TopDocs tds;
            tds = searcher.search(query, new CustomIdFilter(new FilterAccessor() {
                public String[] values() {
//                    return new String[]{"25", "27", "29"};
                    return new String[]{"build.xml.ggn", "build.java", "build.txt"};
                }

                public String getField() {
//                    return "id";
                    return "filename";
                }

                public boolean set() {
                    return true;
//                    return false;
                }
            }), 50);
            for (ScoreDoc sd : tds.scoreDocs) {
                Document doc = searcher.doc(sd.doc);
                System.out.println(sd.doc + ":(" + sd.score + ")" +
                        "[" + doc.get("filename") + "]" + "【" + doc.get("path") + "】--->" +
                        doc.get("size") + "---->" + doc.get("id")
                );
                searcher.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
